﻿// <copyright file="ViewManager.cs" company="ThorIndustry">Copyright (c) ThorIndustry. All rights reserved.</copyright>

using System.Collections.Generic;

public class ViewManager : UnityEngine.MonoBehaviour, IViewService
{
    public UnityEngine.GUISkin GuiSkin;

    private OutGameView outGameView;
    private PuzzleView puzzleView;

    private Dictionary<System.Type, View> viewByType = new Dictionary<System.Type, View>();

    public View CurrentView
    {
        get;
        set;
    }

    public UnityEngine.GUISkin CurrentGuiSkin
    {
        get
        {
            return this.GuiSkin;
        }
    }

    public PaletteDefinition CurrentPaletteDefinition
    {
        get;
        set;
    }

    public void Activate(System.Type viewType)
    {
        if (this.viewByType.ContainsKey(viewType))
        {
            this.Activate(this.viewByType[viewType]);
        }
    }

    public void Activate(View view)
    {
        // If any other view is visible, hide it.
        if (this.CurrentView != null)
        {
            this.CurrentView.Hide();
            this.CurrentView = null;
        }

        // Show the new view.
        if (view != null)
        {
            this.CurrentView = view;
            this.CurrentView.Show();
        }
    }

    protected virtual System.Collections.IEnumerator Start()
    {
        UnityEngine.Debug.Log("ViewManager start.");

        // Wait for game service!
        IGameService gameService = Services.GetService<IGameService>();
        while (gameService == null)
        {
            yield return null;
            gameService = Services.GetService<IGameService>();
        }

        Services.Register<IViewService>(this);

        IDatatableService datatableService = Services.GetService<IDatatableService>();
        IDatatable<PaletteDefinition> paletteDatabase = datatableService.GetDatatable<PaletteDefinition>();
        this.CurrentPaletteDefinition = paletteDatabase.GetValue("Autumn");

        // Get all the views.
        this.viewByType.Add(typeof(WelcomeView), this.GetComponentInChildren<WelcomeView>());
        // this.viewByType.Add(typeof(OutGameView), this.GetComponentInChildren<OutGameView>());
        this.viewByType.Add(typeof(PuzzleView), this.GetComponentInChildren<PuzzleView>());        

        // Initialize all the views.
        foreach (KeyValuePair<System.Type, View> kvp in this.viewByType)
        {
            kvp.Value.Initialize();
        }

        // Load the OutGameView.
        //this.Activate(typeof(OutGameView));
        this.Activate(typeof(WelcomeView));
    }
}
